home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Kant Generator Pro 1.0.1 / source / kode new / kant build gui.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  10.9 KB  |  448 lines  |  [TEXT/MMCC]

  1. #include "kant build gui.h"
  2. #include "kant build meat.h"
  3. #include "kant build files.h"
  4. #include "dialogs.h"
  5. #include "error.h"
  6. #include "util.h"
  7. #include "main.h"
  8. #include "menus.h"
  9. #include "window layer.h"
  10. #include "text twiddling.h"
  11. #include "program globals.h"
  12.  
  13. #define kTextItem            5
  14. #define kEditRefDialog        401
  15. #define kNewRefDialog        402
  16. #define kDeleteAlert        403
  17.  
  18. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  19.  
  20. static    Boolean DoRefDialogDispatch(Boolean isEdit, Boolean isRef, Str255 oldStr, Str255 newStr);
  21. static    pascal Boolean RefModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem);
  22. static    pascal Boolean InstantModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem);
  23. static    Boolean CheckDeleteDialogDispatch(Boolean isRef, Str255 theName);
  24.  
  25. void DoDeleteRef(WindowPtr theWindow)
  26. {
  27.     FSSpec            theFS;
  28.     TEHandle        hTE;
  29.     ControlHandle    vScrollBar;
  30.     Str255            theName;
  31.     short            selStart, selEnd;
  32.     short            lineNum;
  33.     OSErr            oe;
  34.     
  35.     if (!AnyHighlightedQQ(theWindow))
  36.         return;
  37.     
  38.     theFS=GetWindowFS(theWindow);
  39.     hTE=GetWindowTE(theWindow);
  40.     vScrollBar=GetWindowVScrollBar(theWindow);
  41.     GetHighlightedString(theWindow, theName);
  42.     
  43.     if (!CheckDeleteDialogDispatch(TRUE, theName))
  44.         return;
  45.     
  46.     if ((oe=DeleteOneReference(theFS, theName))!=noErr)
  47.     {
  48.         HandleError(kCantDeleteReference, FALSE, FALSE);
  49.         return;
  50.     }
  51.     
  52.     lineNum=CurrentLineNumber(hTE);
  53.     selStart=LineStart(hTE, lineNum);
  54.     selEnd=GetNextInstantiationOffset(theWindow);
  55.     TESetSelect(selStart, selEnd, hTE);
  56.     TEDelete(hTE);
  57.     AdjustForEndScroll(vScrollBar, hTE);
  58.     HighlightLine(hTE, lineNum);
  59.     AdjustVScrollBar(vScrollBar, hTE);
  60.     RebuildReferencesList();
  61. }
  62.  
  63. void DoDeleteInstantiation(WindowPtr theWindow)
  64. {
  65.     FSSpec            theFS;
  66.     TEHandle        hTE;
  67.     ControlHandle    vScrollBar;
  68.     Str255            theName, refName;
  69.     short            stringIndex;
  70.     short            lineNum;
  71.     OSErr            oe;
  72.     
  73.     if (!AnyHighlightedQQ(theWindow))
  74.         return;
  75.     
  76.     theFS=GetWindowFS(theWindow);
  77.     hTE=GetWindowTE(theWindow);
  78.     vScrollBar=GetWindowVScrollBar(theWindow);
  79.     GetHighlightedString(theWindow, theName);
  80.     if (!CheckDeleteDialogDispatch(FALSE, theName))
  81.         return;
  82.     
  83.     GetRefNameFromInstantiation(theWindow, refName, &stringIndex);
  84.     if ((oe=DeleteOneInstantiation(theFS, refName, stringIndex))!=noErr)
  85.     {
  86.         HandleError(kCantDeleteInstantiation, FALSE, FALSE);
  87.         return;
  88.     }
  89.     
  90.     lineNum=CurrentLineNumber(hTE);
  91.     TESetSelect(LineStart(hTE, lineNum), LineStart(hTE, lineNum+1), hTE);
  92.     TEDelete(hTE);
  93.     AdjustForEndScroll(vScrollBar, hTE);
  94.     HighlightLine(hTE, lineNum);
  95.     AdjustVScrollBar(vScrollBar, hTE);
  96. }
  97.  
  98. void DoNewRef(WindowPtr theWindow)
  99. {
  100.     FSSpec            theFS;
  101.     Str255            refName;
  102.     TEHandle        hTE;
  103.     Boolean            addWorked;
  104.     short            lineNum;
  105.     OSErr            oe;
  106.     
  107.     if (!DoRefDialogDispatch(FALSE, TRUE, "\p", refName))
  108.         return;
  109.     
  110.     theFS=GetWindowFS(theWindow);
  111.     if (ReferenceNameExistsQQ(theFS, refName))
  112.     {
  113.         HandleError(kDuplicateReferenceName, FALSE, FALSE);
  114.         return;
  115.     }
  116.     
  117.     if ((oe=AddOneReference(theFS, refName))!=noErr)
  118.     {
  119.         HandleError(kCantCreateReference, FALSE, FALSE);
  120.         return;
  121.     }
  122.     
  123.     hTE=GetWindowTE(theWindow);
  124.     lineNum=CurrentLineNumber(hTE);
  125.     TESetSelect((**hTE).teLength, (**hTE).teLength, hTE);
  126.     addWorked=AddNameToTE(hTE, refName, TRUE);
  127.     if (addWorked)
  128.         lineNum=CurrentLineNumber(hTE);
  129.     HighlightLine(hTE, lineNum);
  130.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  131.     if (addWorked)
  132.         RebuildReferencesList();
  133.     else
  134.         HandleError(kModuleTooLarge, FALSE, FALSE);
  135. }
  136.  
  137. void DoNewInstantiation(WindowPtr theWindow)
  138. {
  139.     FSSpec            theFS;
  140.     Str255            instantName, refName;
  141.     TEHandle        hTE;
  142.     Boolean            addWorked;
  143.     short            dummyShort;
  144.     short            offset;
  145.     short            lineNum;
  146.     OSErr            oe;
  147.     
  148.     if (!DoRefDialogDispatch(FALSE, FALSE, "\p", instantName))
  149.         return;
  150.     
  151.     if (RefHighlightedQQ(theWindow))
  152.         GetHighlightedString(theWindow, refName);
  153.     else
  154.         GetRefNameFromInstantiation(theWindow, refName, &dummyShort);
  155.     
  156.     theFS=GetWindowFS(theWindow);
  157.     if ((oe=AddOneInstantiation(theFS, refName, instantName))!=noErr)
  158.     {
  159.         HandleError(kCantCreateInstantiation, FALSE, FALSE);
  160.         return;
  161.     }
  162.     
  163.     hTE=GetWindowTE(theWindow);
  164.     offset=GetNextInstantiationOffset(theWindow);
  165.     lineNum=CurrentLineNumber(hTE);
  166.     TESetSelect(offset, offset, hTE);
  167.     addWorked=AddNameToTE(hTE, instantName, FALSE);
  168.     if (addWorked)
  169.         lineNum=LineNumberFromOffset(hTE, offset);
  170.     HighlightLine(hTE, lineNum);
  171.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  172.     
  173.     if (!addWorked)
  174.         HandleError(kModuleTooLarge, FALSE, FALSE);
  175. }
  176.  
  177. void DoEditRef(WindowPtr theWindow)
  178. {
  179.     TEHandle        hTE;
  180.     FSSpec            theFS;
  181.     Str255            oldStr, newStr;
  182.     OSErr            oe;
  183.     short            lineNum;
  184.     Boolean            addWorked;
  185.     
  186.     if (!AnyHighlightedQQ(theWindow))
  187.         return;
  188.     
  189.     hTE=GetWindowTE(theWindow);
  190.     theFS=GetWindowFS(theWindow);
  191.     GetHighlightedString(theWindow, oldStr);
  192.     
  193.     if (!DoRefDialogDispatch(TRUE, TRUE, oldStr, newStr))
  194.         return;
  195.     
  196.     if (ReferenceNameExistsQQ(theFS, newStr))
  197.     {
  198.         HandleError(kDuplicateReferenceName, FALSE, FALSE);
  199.         return;
  200.     }
  201.     
  202.     if ((oe=ReplaceOneReference(theFS, oldStr, newStr))!=noErr)
  203.     {
  204.         HandleError(kCantReplaceReference, FALSE, FALSE);
  205.         return;
  206.     }
  207.     
  208.     ZapDrawHook(hTE);
  209.     lineNum=CurrentLineNumber(hTE);
  210.     TESetSelect(LineStart(hTE, lineNum), LineStart(hTE, lineNum+1), hTE);
  211.     TEDelete(hTE);
  212.     RestoreDrawHook(hTE);
  213.     addWorked=AddNameToTE(hTE, newStr, TRUE);
  214.     HighlightLine(hTE, lineNum);
  215.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  216.     if (addWorked)
  217.         RebuildReferencesList();
  218.     else
  219.         HandleError(kModuleTooLarge, FALSE, FALSE);
  220. }
  221.  
  222. void DoEditInstantiation(WindowPtr theWindow)
  223. {
  224.     TEHandle        hTE;
  225.     FSSpec            theFS;
  226.     Str255            oldStr, newStr, refName;
  227.     short            stringIndex;
  228.     OSErr            oe;
  229.     short            lineNum;
  230.     Boolean            addWorked;
  231.     
  232.     if (!AnyHighlightedQQ(theWindow))
  233.         return;
  234.     
  235.     hTE=GetWindowTE(theWindow);
  236.     theFS=GetWindowFS(theWindow);
  237.     GetHighlightedString(theWindow, oldStr);
  238.     GetRefNameFromInstantiation(theWindow, refName, &stringIndex);
  239.         
  240.     if (!DoRefDialogDispatch(TRUE, FALSE, oldStr, newStr))
  241.         return;
  242.     
  243.     if ((oe=ReplaceOneInstantiation(theFS, refName, stringIndex, newStr))!=noErr)
  244.     {
  245.         HandleError(kCantReplaceInstantiation, FALSE, FALSE);
  246.         return;
  247.     }
  248.     
  249.     ZapDrawHook(hTE);
  250.     lineNum=CurrentLineNumber(hTE);
  251.     TESetSelect(LineStart(hTE, lineNum), LineStart(hTE, lineNum+1), hTE);
  252.     TEDelete(hTE);
  253.     RestoreDrawHook(hTE);
  254.     addWorked=AddNameToTE(hTE, newStr, FALSE);
  255.     HighlightLine(hTE, lineNum);
  256.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  257.     
  258.     if (!addWorked)
  259.         HandleError(kModuleTooLarge, FALSE, FALSE);
  260. }
  261.  
  262. /* the rest of these are internal to kant build gui.c */
  263.  
  264. static    Boolean DoRefDialogDispatch(Boolean isEdit, Boolean isRef, Str255 oldStr, Str255 newStr)
  265. /* returns FALSE if cancelled or unchanged */
  266. {
  267.     DialogPtr        theDialog;
  268.     short            itemSelected;
  269.     short            itemType;
  270.     Handle            item;
  271.     Rect            box;
  272.     Boolean            changed;
  273.     Str255            theStr;
  274.     ModalFilterUPP    procFilter;
  275.     short            id;
  276.     
  277.     RemoveHilitePatch();
  278.     
  279.     procFilter=NewModalFilterProc(isRef ? RefModalFilter : InstantModalFilter);
  280.     id=isEdit ? kEditRefDialog : kNewRefDialog;
  281.     PositionDialog('DLOG', id);
  282.     theDialog = GetNewDialog(id, 0L, (WindowPtr)-1L);
  283.     GetDItem(theDialog, 3, &itemType, &item, &box);
  284.     InsetRect(&box, -4, -4);
  285.     SetDItem(theDialog, 3, userItem, (Handle)OutlineDefaultButton, &box);
  286.     SetWTitle(theDialog, isEdit ? (isRef ? "\pEdit reference" : "\pEdit instantiation") :
  287.                                   (isRef ? "\pNew reference" : "\pNew instantiation"));
  288.     ParamText(isRef ? "\preference" : "\pinstantiation", "\p", isRef ? "\pname" : "\ptext", "\p");
  289.     GetDItem(theDialog, kTextItem, &itemType, &item, &box);
  290.     SetIText(item, oldStr);
  291.     SelIText(theDialog, kTextItem, 0, 32767);    /* highlight old name */
  292.     ShowWindow(theDialog);
  293.     SetPort(theDialog);
  294.     
  295.     itemSelected=0;
  296.     while ((itemSelected!=1) && (itemSelected!=2))
  297.     {
  298.         ModalDialog(procFilter, &itemSelected);
  299.     }
  300.     
  301.     changed=FALSE;
  302.     
  303.     if (itemSelected==1)
  304.     {
  305.         GetDItem(theDialog, kTextItem, &itemType, &item, &box);
  306.         GetIText(item, theStr);
  307.         
  308.         if ((isRef) && (theStr[0]==0x00))
  309.             changed=FALSE;
  310.         else
  311.         {
  312.             changed=(!Mymemcompare((Ptr)oldStr, (Ptr)theStr, oldStr[0]+1));
  313.             if (changed)
  314.             {
  315.                 Mymemcpy(newStr, theStr, theStr[0]+1);    /* return new name */
  316.             }
  317.         }
  318.     }
  319.     
  320.     HideWindow(theDialog);
  321.     DisposeDialog(theDialog);
  322.     DisposeRoutineDescriptor(procFilter);
  323.     
  324.     InstallHilitePatch();
  325.     
  326.     return changed;
  327. }
  328.  
  329. static    pascal Boolean RefModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  330. {
  331.     unsigned char    theChar;
  332.     
  333.     switch (theEvent->what)    /* examine event record */
  334.     {
  335.         case keyDown:    /* keypress */
  336.         case autoKey:
  337.             theChar=theEvent->message & charCodeMask;    /* get ascii char value */
  338.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  339.             {
  340.                 *theItem=FakeSelect(theDialog, sfItemOpenButton);
  341.                 return TRUE;
  342.             }
  343.             else if ((theChar==0x1b) || (theChar=='`'))    /* escape or ` key */
  344.             {
  345.                 *theItem=FakeSelect(theDialog, sfItemCancelButton);
  346.                 return TRUE;
  347.             }
  348.             else if (theEvent->modifiers & cmdKey)
  349.             {
  350.                 switch (theChar)
  351.                 {
  352.                     case '.':
  353.                         *theItem=FakeSelect(theDialog, 2);
  354.                         return TRUE;
  355.                         break;
  356.                 }
  357.             }
  358.             if ((theChar>=key_LeftArrow) && (theChar<=key_DownArrow))
  359.                 return FALSE;
  360.             if (theChar==0x08)
  361.                 return FALSE;
  362.             if ((theChar>='a') && (theChar<='z'))
  363.                 return FALSE;
  364.             if ((theChar>='A') && (theChar<='Z'))
  365.                 return FALSE;
  366.             if ((theChar>='0') && (theChar<='9'))
  367.                 return FALSE;
  368.             if ((theChar=='-') || (theChar=='_'))
  369.                 return FALSE;
  370.             if (theChar==' ')
  371.             {
  372.                 theEvent->message='-';
  373.                 return FALSE;
  374.             }
  375.             
  376.             theEvent->what=nullEvent;
  377.             break;
  378.         case updateEvt:
  379.             if ((theEvent->message)!=(unsigned long)theDialog)
  380.             {
  381.                 DispatchEvents(*theEvent, FALSE);
  382.                 return TRUE;
  383.             }
  384.             break;
  385.     }
  386.     
  387.     return FALSE;    /* no faking, proceed as planned */
  388. }
  389.  
  390. static    pascal Boolean InstantModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  391. {
  392.     unsigned char    theChar;
  393.     
  394.     switch (theEvent->what)    /* examine event record */
  395.     {
  396.         case keyDown:    /* keypress */
  397.         case autoKey:
  398.             theChar=theEvent->message & charCodeMask;    /* get ascii char value */
  399.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  400.             {
  401.                 *theItem=FakeSelect(theDialog, sfItemOpenButton);
  402.                 return TRUE;
  403.             }
  404.             else if ((theChar==0x1b) || (theChar=='`'))    /* escape or ` key */
  405.             {
  406.                 *theItem=FakeSelect(theDialog, sfItemCancelButton);
  407.                 return TRUE;
  408.             }
  409.             else if (theEvent->modifiers & cmdKey)
  410.             {
  411.                 switch (theChar)
  412.                 {
  413.                     case '.':
  414.                         *theItem=FakeSelect(theDialog, 2);
  415.                         return TRUE;
  416.                         break;
  417.                 }
  418.             }
  419.             break;
  420.         case updateEvt:
  421.             if ((theEvent->message)!=(unsigned long)theDialog)
  422.             {
  423.                 DispatchEvents(*theEvent, FALSE);
  424.                 return TRUE;
  425.             }
  426.             break;
  427.     }
  428.     
  429.     return FALSE;    /* no faking, proceed as planned */
  430. }
  431.  
  432. static    Boolean CheckDeleteDialogDispatch(Boolean isRef, Str255 theName)
  433. {
  434.     ModalFilterUPP    procFilter = NewModalFilterProc(TwoButtonFilter);
  435.     short            result;
  436.     
  437.     RemoveHilitePatch();
  438.     
  439.     PositionDialog('ALRT', kDeleteAlert);
  440.     ParamText(isRef ? "\preference" : "\pinstantiation", theName, "\p", "\p");
  441.     result=CautionAlert(kDeleteAlert, procFilter);
  442.     DisposeRoutineDescriptor(procFilter);
  443.     
  444.     InstallHilitePatch();
  445.     
  446.     return (result==1);
  447. }
  448.